import * as THREE from 'three';
// 获取模型底部四个边位置信息
export function getBoxHelperEdgeMidpointsOnScreen(object, camera) {
  const box = new THREE.Box3().setFromObject(object);
  // 获取Box的八个顶点坐标
  const vertices = [
    box.min.clone(),
    new THREE.Vector3(box.max.x, box.min.y, box.min.z),
    new THREE.Vector3(box.min.x, box.max.y, box.min.z),
    new THREE.Vector3(box.min.x, box.min.y, box.max.z),
    new THREE.Vector3(box.max.x, box.max.y, box.min.z),
    new THREE.Vector3(box.max.x, box.min.y, box.max.z),
    new THREE.Vector3(box.min.x, box.max.y, box.max.z),
    box.max.clone(),
  ];

  const edges = [
    [vertices[0], vertices[1]],
    // [vertices[0], vertices[2]],
    [vertices[0], vertices[3]],
    // [vertices[1], vertices[4]],
    [vertices[1], vertices[5]],
    // [vertices[2], vertices[4]],
    // [vertices[2], vertices[6]],
    [vertices[3], vertices[5]],
    // [vertices[3], vertices[6]],
    // [vertices[4], vertices[7]],
    // [vertices[5], vertices[7]],
    // [vertices[6], vertices[7]],
  ];

  const edgeMidpointsOnScreen = [];
  edges.forEach(edge => {
    const midpoint = edge[0]
      .clone()
      .add(edge[1])
      .multiplyScalar(0.5);
    midpoint.project(camera);
    const x = (midpoint.x * window.innerWidth) / 2 + window.innerWidth / 2;
    const y = -((midpoint.y * window.innerHeight) / 2) + window.innerHeight / 2;
    edgeMidpointsOnScreen.push({ x, y });
  });

  // 计算顶部面的中点位置
  const topFaceVertices = [vertices[2], vertices[6], vertices[4], vertices[7]];
  const topFaceMidpoint = new THREE.Vector3();
  topFaceVertices.forEach(vertex => {
    topFaceMidpoint.add(vertex);
  });
  topFaceMidpoint.divideScalar(topFaceVertices.length);
  topFaceMidpoint.project(camera);
  const topFaceX =
    (topFaceMidpoint.x * window.innerWidth) / 2 + window.innerWidth / 2;
  const topFaceY =
    -((topFaceMidpoint.y * window.innerHeight) / 2) + window.innerHeight / 2;
  edgeMidpointsOnScreen.push({ x: topFaceX, y: topFaceY });

  return edgeMidpointsOnScreen;
}
